<template>
  <el-submenu
    v-if="menu.list && menu.list.length >= 1"
    :index="menu.id + ''"
    :popper-class="'site-sidebar--' + sidebarLayoutSkin + '-popper'"
  >
    <template slot="title">
      <icon-svg
        :name="menu.icon || ''"
        class="site-sidebar__menu-icon"
      ></icon-svg>
      <span>{{ menu.menuName }}</span>
    </template>
    <sub-menu
      v-for="item in menu.list"
      :key="item.id"
      :menu="item"
      :dynamicMenuRoutes="dynamicMenuRoutes"
    >
    </sub-menu>
  </el-submenu>
  <el-menu-item v-else :index="menu.url + ''" @click="gotoRouteHandle(menu)">
    <icon-svg
      :name="menu.icon || ''"
      class="site-sidebar__menu-icon"
    ></icon-svg>
    <span>{{ menu.menuName }}</span>
  </el-menu-item>
</template>

<script>
import SubMenu from "./main-sidebar-sub-menu";
export default {
  name: "sub-menu",
  props: {
    menu: {
      type: Object,
      required: true
    },
    dynamicMenuRoutes: {
      type: Array,
      required: true
    }
  },
  components: {
    SubMenu
  },
  computed: {
    sidebarLayoutSkin: {
      get() {
        return this.$store.state.common.sidebarLayoutSkin;
      }
    }
  },
  methods: {
    // 通过url与动态(菜单)路由进行匹配跳转至指定路由
    gotoRouteHandle(menu) {
      let route = this.dynamicMenuRoutes.find(i => {
        return i.name === menu.url;
      });
      this.$router.push({ name: route.name });
    }
  }
};
</script>
